const mongoose=require("mongoose")
var Food=mongoose.model("food")

exports.findFoodsByPage=function(req,res){
	if(parseInt(req.params.pageNo) ==0){
		res.json(null)
	}else{
		Food.find({})
		.skip((parseInt(req.params.pageNo)-1)*4).limit(4)
		.exec(function(err,foods){
			if (!err) {
				res.json(foods);
			} else {
				res.json({error:"findFoodsByPage"})
			}
		})
	}
}

exports.updateFoodStatus=function(req,res){
	Food.findByIdAndUpdate(req.params.fId,{fStatus:req.params.fStatus},function(err,oldFood){
		if (!err) {
			res.send("success")
		} else {
			res.json({error:"updateFoodStatus"})
		}
	})
}

exports.findAllRecFoodsId=function(req,res){
	Food.find({fStatus:3},"_id",function(err,ids){
		if (!err) {
			var results=[];
			for (var item in ids) {
				results.push(ids[item]._id)
			}
			res.send(results.toString()+",")
		} else {
			res.json({error:"findAllRecFoodsId"})
		}
	})
}

